﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.IO;
using System.Diagnostics;

namespace SharePointOfView.SPReporting.EventReceivers
{
    /// <summary>
    /// Feature's Receiver for list instances creation
    /// </summary>
    public class SPReportingWebFeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// On feature's activation, check if lists (reports and models library) are created or not and create them if needed.
        /// </summary>
        /// <param name="properties">Context Properties</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = properties.Feature.Parent as SPWeb;
            if (web != null)
            {
                int defaultMajorVersions = 0;
                if (properties.Feature.Properties["DefaultMajorVersions"] != null)
                {
                    defaultMajorVersions = int.Parse(properties.Feature.Properties["DefaultMajorVersions"].Value);
                }

                string reportingModelsUrl = Utilities.GetResourceString("SoVSPReportingLists_ReportingModelsList_Url", "SoVSPReporting", web.Language);
                string reportingReportsUrl = Utilities.GetResourceString("SoVSPReportingLists_ReportingReportsList_Url", "SoVSPReporting", web.Language);
                SPList reportingModelsList = null;
                SPList reportingReportsList = null;

                // Create list instances (reports & reports models) if not present
                try
                {
                    reportingModelsList = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, reportingModelsUrl));
                }
                catch (FileNotFoundException)
                {
                    string reportingModelsTitle = Utilities.GetResourceString("SoVSPReportingLists_ReportingModelsList_Title", "SoVSPReporting", web.Language);
                    string reportingModelsDescription = Utilities.GetResourceString("SoVSPReportingLists_ReportingModelsList_Description", "SoVSPReporting", web.Language);

                    // Exception raised because list doesn't exist => create it
                    web.Lists.Add(reportingModelsTitle, reportingModelsDescription, reportingModelsUrl, Globals.SPReportingWebFeatureId.ToString(), Globals.ReportingModelsTemplateType, "100", SPListTemplate.QuickLaunchOptions.On);
                    Debug.Write("List \"" + reportingModelsTitle + "\" created", "SharePointOfView.SPReporting.EventReceivers.SPReportingWebFeatureReceiver.FeatureActivated");
                }
                catch (SPException spex)
                {
                    Debug.Write("SharePoint Error : " + spex.Message, "SharePointOfView.SPReporting.EventReceivers.SPReportingWebFeatureReceiver.FeatureActivated");
                }

                try
                {
                    reportingReportsList = web.GetList(SPUrlUtility.CombineUrl(web.ServerRelativeUrl, reportingReportsUrl));
                }
                catch (FileNotFoundException)
                {
                    string reportingReportsTitle = Utilities.GetResourceString("SoVSPReportingLists_ReportingReportsList_Title", "SoVSPReporting", web.Language);
                    string reportingReportsDescription = Utilities.GetResourceString("SoVSPReportingLists_ReportingReportsList_Description", "SoVSPReporting", web.Language);

                    // Exception raised because list doesn't exist => create it
                    Guid listId = web.Lists.Add(reportingReportsTitle, reportingReportsDescription, reportingReportsUrl, "00BFEA71-E717-4E80-AA17-D0C71B360101", 101, "100", SPListTemplate.QuickLaunchOptions.Off);
                    Debug.Write("List \"" + reportingReportsTitle + "\" created", "SharePointOfView.SPReporting.EventReceivers.SPReportingWebFeatureReceiver.FeatureActivated");
                    if (defaultMajorVersions > 0)
                    {
                        //web.Update();
                        reportingReportsList = web.Lists[listId];
                        reportingReportsList.EnableVersioning = true;
                        reportingReportsList.MajorVersionLimit = defaultMajorVersions;
                        reportingReportsList.Update();
                        Debug.Write(string.Format("List \"" + reportingReportsTitle + "\" has now versioning enabled ({0} major versions)", defaultMajorVersions), "SharePointOfView.SPReporting.EventReceivers.SPReportingWebFeatureReceiver.FeatureActivated");
                    }
                }
                catch (SPException spex)
                {
                    Debug.Write("SharePoint Error : " + spex.Message, "SharePointOfView.SPReporting.EventReceivers.SPReportingWebFeatureReceiver.FeatureActivated");
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
